import boto3
import asyncio
from ecr_billing_tools import generate_date_range
import ecr_billing_link_cur
import ecr_billing_monthly_patch_ob
import ecr_billing_sync_to_customer_cur

def handler(event, context):
    methods = event['methods']
    customer_name = event.get('customer_name')
    # 更新 event 数据
    event = set_event_default_data(event, customer_name)
    billing_system_url = event.get('billing_system_url')
    billing_system_username = event.get('billing_system_username')
    billing_system_password = event.get('billing_system_password')
    billing_system_sub_cust_list = event.get('billing_system_sub_cust_list')
    billing_system_month = event.get('billing_system_month')
    last_cur_bucket = event.get('last_cur_bucket')
    last_cur_prefix_base = event.get('last_cur_prefix_base')
    last_cur_prefix_month = event.get('last_cur_prefix_month')
    link_cur_bucket = event.get('link_cur_bucket')
    link_cur_prefix_base = event.get('link_cur_prefix_base')
    customer_cur_bucket = event.get('customer_cur_bucket')
    aws_role_arn = event.get('aws_role_arn')
    aws_access_key_id = event.get('aws_access_key_id')
    aws_secret_access_key = event.get('aws_secret_access_key')

    s3 = boto3.client('s3', aws_access_key_id = aws_access_key_id, aws_secret_access_key = aws_secret_access_key)
    #billing_system_month, last_cur_prefix_month = generate_date_range() 
    manifest_json_name = 'ob-payer-cur-Manifest.json'

    returnStr = '准备开始执行函数：'

    if 'download_link_cur' in methods:
        asyncio.run(ecr_billing_link_cur.download(billing_system_url, billing_system_username, billing_system_password, billing_system_month, billing_system_sub_cust_list, link_cur_bucket, link_cur_prefix_base, s3))
        returnStr += f"Atlas 的 Link Cur 下载完毕，文件上传至 s3://{link_cur_bucket}{link_cur_prefix_base}。"
    
    if 'monthly_patch' in methods:
        if customer_name is None or customer_name == 'ob':
            ecr_billing_monthly_patch_ob.process_patch(last_cur_prefix_month, last_cur_prefix_base, link_cur_prefix_base, last_cur_bucket, link_cur_bucket, manifest_json_name, s3)
            returnStr += f"针对客户【{customer_name}】的 monthly_patch 函数执行完成。"
        else :
            returnStr += "没有匹配到客户名称, monthly_patch 函数未执行。"

    if 'sync_to_cur' in methods:
        if customer_name is None or customer_name == 'ob':
            last_cur_prefix_parent = f'{last_cur_prefix_base}{last_cur_prefix_month}/'
            new_last_cur_prefix = ecr_billing_monthly_patch_ob.generate_last_folder(last_cur_prefix_parent, last_cur_bucket, s3)
            ecr_billing_sync_to_customer_cur.sync_to_cur_bucket_ob(aws_role_arn, last_cur_bucket, customer_cur_bucket, manifest_json_name, last_cur_prefix_parent, new_last_cur_prefix)
            returnStr += f"针对客户【{customer_name}】的 sync_to_cur 函数执行完成。/n"
        else:
            returnStr += "没有匹配到客户名称, sync_to_cur 函数未执行。"

    return {
        'statusCode': 200,
        'body': f'{returnStr}'
    }

def set_event_default_data(event, customer_name):
    return event